package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianServerScoreData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class CrossXiuXianServerScoreDataDaoImpl extends TempDataObjectDao<CrossXiuXianServerScoreData> {

    public static String getCrossXiuXianServerScoreDataSql = "select * from `t_c_xiuxian_server_score` where `activityId`=? ";

    /**
     * 获取区服采集数据
     */
    public List<CrossXiuXianServerScoreData> getCrossXiuXianServerScoreDataList(int activityId) {
        List<CrossXiuXianServerScoreData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossXiuXianServerScoreDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXiuXianServerScoreData parse = parse(rs);
                list.add(parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertCrossXiuXianServerScoreDataSql = "insert into `t_c_xiuxian_server_score` (uuid,activityId,serverIdList,serverScore) values (?,?,?,?)";

    @Override
    protected boolean addData(CrossXiuXianServerScoreData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUuid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerScore()));
        if (getDb().executeNoneQuery(insertCrossXiuXianServerScoreDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossXiuXianServerScoreDataSql = "update `t_c_xiuxian_server_score` set serverIdList=?,serverScore=? where activityId=? and uuid=?";

    @Override
    protected boolean updateData(CrossXiuXianServerScoreData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUuid()));
        if (getDb().executeNoneQuery(updateCrossXiuXianServerScoreDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossXiuXianServerScoreData parseData(ResultSet rs) throws SQLException {
        CrossXiuXianServerScoreData data = new CrossXiuXianServerScoreData();
        data.setUuid(rs.getString("uuid"));
        data.setActivityId(rs.getInt("activityId"));
        data.setServerIdList(JSON.parseObject(rs.getString("serverIdList"), new TypeReference<Set<Long>>() {
        }));
        data.setServerScore(rs.getLong("serverScore"));
        return data;
    }
}
